<template>
  <div>
    <v-viewer ref="view"></v-viewer>
    <div class="order-area">
      <div class="customer-area">
        <div class="title">
          <div class="point"></div>
          <span>基础信息</span>
        </div>
        <el-form size="medium"
                 label-position="right"
                 label-width="100px">
          <el-form-item label="客户">
            <el-select v-model="customer.id"
                       filterable
                       placeholder="请选择客户"
                       @change="setCustomerDetail(customer.id)">
              <el-option v-for="(item, key) in customerList"
                         :key="key"
                         :label="item.name"
                         :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="客户姓名">
            <el-col :span="7">
              <el-input v-model="customer.customerName"
                        placeholder="客户姓名"
                        size="medium"></el-input>
            </el-col>
            <el-col :span="10">
              <el-form-item label="性别">
                <el-radio v-model="customer.gender"
                          border
                          label='男'>男</el-radio>
                <el-radio v-model="customer.gender"
                          border
                          label='女'>女</el-radio>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="客户手机号">
            <el-col :span="7">
              <el-input v-model.number="customer.customerTel"
                        placeholder="客户手机号"
                        size="medium"></el-input>
            </el-col>
            <el-col :span="10">
              <el-form-item label="客户地址">
                <el-input v-model="customer.address"
                          placeholder="客户地址"
                          size="medium"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="客户来源">
            <el-col :span="7">
              <el-input v-model="customer.customerSource"
                        placeholder="客户来源"
                        size="medium"></el-input>
            </el-col>
            <el-col :span="10">
              <el-form-item label="客户邮箱">
                <el-input v-model="customer.email"
                          placeholder="客户邮箱"
                          size="medium"></el-input>
              </el-form-item>
            </el-col>
          </el-form-item>
          <template v-if="orderHeader.some(a => a.key === 'total_money')">
            <el-form-item label="合同金额">
              <el-col :span="7">
                <el-input v-model.number="order.orderAmount"
                          placeholder="合同金额"
                          @change="() => {order.finCost = (order.orderAmount || 0) - (order.customerPay || 0)}"
                          size="medium"></el-input>
              </el-col>
              <el-col :span="10">
                <el-form-item label="客户付款">
                  <el-input v-model.number="order.customerPay"
                            placeholder="客户付款"
                            @change="() => {order.finCost = (order.orderAmount || 0) - (order.customerPay || 0)}"
                            size="medium"></el-input>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="客户尾款">
              <el-col :span="7">
                <el-input v-model.number="order.finCost"
                          placeholder="客户尾款"
                          size="medium"></el-input>
              </el-col>
              <el-col :span="10">
                <el-form-item label="尾款支付方式">
                  <el-radio v-model="order.finCostType"
                            border
                            label=0>配送前</el-radio>
                  <el-radio v-model="order.finCostType"
                            border
                            label=1>配送后</el-radio>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="签约时间">
              <el-col :span="7">
                <el-date-picker v-model="order.signedTime"
                                type="date"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd"
                                placeholder="签约时间">
                </el-date-picker>
              </el-col>
              <el-col :span="10">
                <el-form-item label="合同截至日">
                  <el-date-picker v-model="order.endedTime"
                                  type="date"
                                  format="yyyy-MM-dd"
                                  value-format="yyyy-MM-dd"
                                  placeholder="合同截至日">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="下单时间">
              <el-col :span="7">
                <el-date-picker v-model="order.sureOrderTime"
                                type="date"
                                format="yyyy-MM-dd"
                                value-format="yyyy-MM-dd"
                                placeholder="下单时间">
                </el-date-picker>
              </el-col>
              <el-col :span="10">
                <el-form-item label="回访时间">
                  <el-date-picker v-model="order.reportTime"
                                  type="date"
                                  format="yyyy-MM-dd"
                                  value-format="yyyy-MM-dd"
                                  placeholder="回访时间">
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-form-item>
            <el-form-item label="回访意见">
              <el-col :span="7">
                <el-input v-model="order.reportAdvice"
                          placeholder="回访意见"
                          size="medium"></el-input>
              </el-col>
            </el-form-item>
          </template>
        </el-form>
      </div>
    </div>
    <div class="customer-area">
      <div class="title">
        <div class="point"></div>
        <span>资料</span>
      </div>
      <el-row :gutter="10">
        <el-col :span="3">
          <div class="content-title">
            <span>客户合同</span>
          </div>
        </el-col>
        <el-col :span="19">
          <div>
            <pic-view :file-list="order.contractUrlList"
                      upload-class="short-upload"
                      :pic-name-list.sync="order.contractNameList"
                      :max-size="5"
                      :upload-data="{ fileName: 'contract' }"
                      @showImg="showImg"></pic-view>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="3">
          <div class="content-title">
            <span>客户收支证明</span>
          </div>
        </el-col>
        <el-col :span="19">
          <div>
            <pic-view :file-list="order.incomeUrlList"
                      upload-class="short-upload"
                      :pic-name-list.sync="order.incomeNameList"
                      :max-size="5"
                      :upload-data="{ fileName: 'income' }"
                      @showImg="showImg"></pic-view>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="3">
          <div class="content-title">
            <span>下单通过凭证</span>
          </div>
        </el-col>
        <el-col :span="19">
          <div>
            <pic-view :file-list="order.passCerUrlList"
                      upload-class="short-upload"
                      :pic-name-list.sync="order.passCerNameList"
                      :upload-data="{ fileName: 'passCer' }"
                      :max-size="10"
                      @showImg="showImg"></pic-view>
          </div>
        </el-col>
      </el-row>
      <template v-if="orderHeader.some(a => a.key === 'total_money')">
        <el-row :gutter="10">
          <el-col :span="3">
            <div class="content-title">
              <span>尾款凭证</span>
            </div>
          </el-col>
          <el-col :span="19">
            <div>
              <pic-view :file-list="order.finCostUrlList"
                        upload-class="short-upload"
                        :pic-name-list.sync="order.finCostNameList"
                        :max-size="5"
                        :upload-data="{ fileName: 'finalCost' }"
                        @showImg="showImg"></pic-view>
            </div>
          </el-col>
        </el-row>
      </template>
      <el-row :gutter="10">
        <el-col :span="3">
          <div class="content-title">
            <span>货物详细</span>
          </div>
        </el-col>
        <el-col :span="2">
          <el-button size="medium"
                     type="primary"
                     @click="addChildOrder">增加</el-button>
        </el-col>
        <el-col :span="5">
          <el-upload :action="uploadExcelAction"
                     multiple
                     :limit="1"
                     :headers="{ Authorization: getToken() }"
                     :on-exceed="handleExceed"
                     :on-success="uploadSuccess"
                     :file-list="order.fileList">
            <el-button size="medium"
                       type="danger">重新导入</el-button>
            <span style="color:red;">(重新导入会覆盖掉之前的记录请谨慎操作)</span>
          </el-upload>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="19"
                :push="3">
          <el-row style="margin-bottom:20px;">
            <template v-if="orderHeader.some(a => a.key === 'total_money')">
              <el-col :span="24">
                <div class="el-table-total">
                  <span>合同金额&nbsp;{{ order.orderAmount || 0}}</span>&nbsp;&nbsp;&nbsp;
                  <span v-if="order.childOrderAmount">货物总成本&nbsp;{{ order.childOrderAmount | total(childOrders.map(a => a.productCost || 0).concat(childOrders.map(a => a.extraCost || 0))) }}</span>
                </div>
              </el-col>
            </template>
            <el-col :span="24">
              <el-collapse accordion
                           v-if="childOrders && childOrders.length > 0">
                <el-collapse-item v-for="(item, key) in childOrders"
                                  :key="key"
                                  :title="item.title | productTitle2(item)"
                                  :name="key">
                  <template>
                    <el-row>
                      <div style="margin:10px 0 20px 10px;">
                        <el-button size="medium "
                                   type="danger"
                                   @click="deleteChildOrder(key)">删除</el-button>
                      </div>
                    </el-row>
                    <div style="margin-bottom:20px;">
                      <el-table ref="multipleTable"
                                border
                                :data="[item]"
                                highlight-current-row
                                class="tb-edit"
                                style="width: 100%"
                                @row-click="handleCurrentChange">
                        <el-table-column label="编号">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.itemNo"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.itemNo}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="区域">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.area"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.area}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="品名">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.productName"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.productName}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="品牌">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.brandName"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.brandName}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="产品型号">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.sku"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.sku}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="尺寸">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.size"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.size}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="订货数量">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.quantity"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.quantity}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column v-if="childOrderHeader.some(a => a.key == 'price')"
                                         label="单价">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.price"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>￥{{scope.row.price}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column v-if="childOrderHeader.some(a => a.key == 'total_money')"
                                         label="总价">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.totalMoney"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>￥{{scope.row.totalMoney}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column label="主面料"
                                         width="130">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.mainFabric"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.mainFabric}}</span>
                            <pic-view :file-list="scope.row.bodyUrlList"
                                      upload-class="small-upload"
                                      :pic-name-list.sync="scope.row.bodyNameList"
                                      :max-size="2"
                                      :upload-data="{ fileName: 'product' }"
                                      @showImg="showImg"></pic-view>
                          </template>
                        </el-table-column>
                        <el-table-column label="抱枕面料"
                                         width="130">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.bolsterFabric"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.bolsterFabric}}</span>
                            <pic-view :file-list="scope.row.pillowUrlList"
                                      upload-class="small-upload"
                                      :pic-name-list.sync="scope.row.pillowNameList"
                                      :max-size="2"
                                      :upload-data="{ fileName: 'product' }"
                                      @showImg="showImg"></pic-view>
                          </template>
                        </el-table-column>
                        <el-table-column label="主面料木质部分材质"
                                         width="130">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.woodTexture"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.woodTexture}}</span>
                            <pic-view :file-list="scope.row.woodenUrlList"
                                      upload-class="small-upload"
                                      :pic-name-list.sync="scope.row.woodenNameList"
                                      :max-size="2"
                                      :upload-data="{ fileName: 'product' }"
                                      @showImg="showImg"></pic-view>
                          </template>
                        </el-table-column>
                        <el-table-column label="其他材质备注"
                                         width="130">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model="scope.row.otherTexture"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.otherTexture}}</span>
                            <pic-view :file-list="scope.row.otherUrlList"
                                      upload-class="small-upload"
                                      :pic-name-list.sync="scope.row.otherNameList"
                                      :max-size="2"
                                      :upload-data="{ fileName: 'product' }"
                                      @showImg="showImg"></pic-view>
                          </template>
                        </el-table-column>
                        <el-table-column label="供货方订单编号">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.manufactorCode"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>{{scope.row.manufactorCode}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column v-if="childOrderHeader.some(a => a.key == 'product_cost')"
                                         label="货物成本">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.productCost"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>￥{{scope.row.productCost}}</span>
                          </template>
                        </el-table-column>
                        <el-table-column v-if="childOrderHeader.some(a => a.key == 'other_cost')"
                                         label="额外成本">
                          <template slot-scope="scope">
                            <el-input size="small"
                                      v-model.number="scope.row.extraCost"
                                      placeholder=""
                                      @change="handleEdit(scope.$index, scope.row)"></el-input> <span>￥{{scope.row.extraCost}}</span>
                          </template>
                        </el-table-column>
                      </el-table>
                    </div>
                  </template>
                  <el-form>
                    <el-form-item label="官网截图：">
                      <pic-view :file-list="item.officialUrlList"
                                upload-class="lang-upload"
                                :pic-name-list.sync="item.officialNameList"
                                :max-size="2"
                                :upload-data="{ fileName: 'official' }"
                                @showImg="showImg"></pic-view>
                    </el-form-item>
                    <el-form-item label="产品图片：">
                      <pic-view :file-list="item.productUrlList"
                                upload-class="lang-upload"
                                :pic-name-list.sync="item.productNameList"
                                :max-size="2"
                                :upload-data="{ fileName: 'product' }"
                                @showImg="showImg"></pic-view>
                    </el-form-item>
                  </el-form>
                  <el-form :inline="true">
                    <el-form-item label="付款时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.paymentTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="付款时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                    <el-form-item label="货好时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.goodsFinishTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="货好时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                    <el-form-item label="预计到港时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.arrivalTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="预计到港时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                    <el-form-item label="入库时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.laidUpTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="入库时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                    <el-form-item label="配送时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.sendTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="配送时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                    <el-form-item label="完成时间：">
                      <el-col :span="10">
                        <el-date-picker v-model="item.finishedTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="完成时间">
                        </el-date-picker>
                      </el-col>
                    </el-form-item>
                  </el-form>
                  <el-form>
                    <el-form-item label="问题：">
                      <pic-view :file-list="item.problemUrlList"
                                upload-class="short-upload"
                                :pic-name-list.sync="item.problemNameList"
                                :upload-data="{ fileName: 'problem' }"
                                @showImg="showImg"></pic-view>
                    </el-form-item>
                    <el-form :inline="true">
                      <el-form-item label="问题描述：">
                        <el-input size="small"
                                  v-model.number="item.problem"
                                  placeholder=""></el-input>
                      </el-form-item>
                      <el-form-item label="时间：">
                        <el-date-picker v-model="item.solvedTime"
                                        type="date"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        placeholder="问题时间">
                        </el-date-picker>
                      </el-form-item>
                      <el-form-item label="解决方案：">
                        <el-input size="small"
                                  v-model.number="item.solvedPlan"
                                  placeholder=""></el-input>
                      </el-form-item>
                    </el-form>
                  </el-form>
                </el-collapse-item>
              </el-collapse>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-row :gutter="10"
              style="margin-bottom:50px;">
        <el-col :span="3">
          <div class="content-title">
            <span>订单备注</span>
          </div>
        </el-col>
        <el-col :span="15">
          <el-input type="textarea"
                    v-model="order.remark"
                    placeholder="请输入"></el-input>
        </el-col>
      </el-row>
      <div class="foot">
        <el-button type="primary"
                   @click="modifyAll">保存</el-button>
        <el-button @click="() => {this.$router.go(-1)}">取消</el-button>
      </div>
    </div>
  </div>
</template>

<style>
.el-table-total {
  float: right;
  width: 30%;
  height: 34px;
  border: 1px solid #ebeef5;
  cursor: pointer;
  justify-content: center;
  display: flex;
  line-height: 34px;
}
</style>

<script>
import { mapGetters } from 'vuex'
import { getToken, getUser } from '@/utils/auth'
import '@/style/info.scss'
import picView from '@/components/pic-viewer-up'
import vViewer from '@/components/v-viewer'
import '@/style/common.scss'
import viewPage from '@/components/pagination'
import { HTTPSUCCESS, HTTPDATAFAIL, HTTPPARAMFAIL, HTTPUNKOWNFAIL } from '@/components/constant'
import { api, devBaseURL } from '@/api'
import { packageImgList } from '@/utils/imgPackage'
import { validatenull, vListLength } from '@/utils/validate'
import { total, productTitle2 } from '@/utils/filters'

export default {
  data() {
    return {
      uploadExcelAction: devBaseURL + api.CHILD_ORDER_IMPORT,
      buttonAuthority: [],
      orderHeader: [],
      childOrderHeader: [],
      orderId: this.$route.params.orderId || null,
      customerList: [],
      planDialog: false,
      customer: {},
      order: {},
      orderCustomerInfo: {},
      childOrders: []
    }
  },
  components: {
    picView, vViewer, viewPage
  },
  computed: {
    ...mapGetters([
      'authority'
    ])
  },
  activated() {
    this.setAuthority()
    this.getOrder()
    this.getCustomerList()
  },
  methods: {
    getUser,
    getToken,
    total,
    setAuthority() {
      this.buttonAuthority = this.authority.buttonAuthority
      this.orderHeader = this.authority.orderHeader
      this.childOrderHeader = this.authority.childOrderHeader
    },
    setCustomerDetail(id) {
      const info = this.customerList.filter(a => a.id === id)[0]
      this.customer = {
        id: info.id,
        customerName: info.name,
        gender: info.gender === 'MALE' ? '男' : '女',
        customerTel: info.mobilePhone,
        address: info.address,
        customerSource: info.source,
        email: info.email
      }
    },
    getCustomerList() {
      this.$http.get(api.CUSTOMER_SELECT_LIST).then(
        res => {
          if (res.error.code === HTTPSUCCESS) {
            this.customerList = res.result
          } else {
            this.$msg._message(res.error.message, 'error')
          }
        }
      )
    },
    getOrder() {
      if (!validatenull(this.orderId)) {
        this.$http.get(api.ORDER_DETAIL, { orderId: this.orderId }).then(
          res => {
            if (res.error.code === HTTPSUCCESS) {
              this.order = res.result.order
              this.customer = res.result.customer
              this.childOrders = res.result.childOrders
              this.orderAmount = this.order.orderAmount
              this.customerPay = this.order.customerPay
              this.finCost = this.order.finCost
              this.order.finCostType = this.order.finCostType + ''
              if (this.order.contract) {
                const contractList = packageImgList(this.order.contract)
                this.order.contractNameList = contractList[0]
                this.order.contractUrlList = contractList[1]
              }
              if (this.order.income) {
                const contractList = packageImgList(this.order.income)
                this.order.incomeNameList = contractList[0]
                this.order.incomeUrlList = contractList[1]
              }
              if (this.order.passProof) {
                const passProofList = packageImgList(this.order.passProof)
                this.order.passCerNameList = passProofList[0]
                this.order.passCerUrlList = passProofList[1]
              }
              if (this.order.finCostPicture) {
                const finCostPictureList = packageImgList(this.order.finCostPicture)
                this.order.finCostNameList = finCostPictureList[0]
                this.order.finCostUrlList = finCostPictureList[1]
              }
              this.childOrders.forEach(e => {
                if (e.officialPicture) {
                  const officialList = packageImgList(e.officialPicture)
                  e.officialNameList = officialList[0]
                  e.officialUrlList = officialList[1]
                }
                if (e.productPicture) {
                  const productList = packageImgList(e.productPicture)
                  e.productNameList = productList[0]
                  e.productUrlList = productList[1]
                }
                if (e.mainFabricPicture) {
                  const bodyList = packageImgList(e.mainFabricPicture)
                  e.bodyNameList = [e.mainFabricPicture]
                  e.bodyUrlList = bodyList[1]
                }
                if (e.bolsterFabricPicture) {
                  const pillowList = packageImgList(e.bolsterFabricPicture)
                  e.pillowNameList = [e.bolsterFabricPicture]
                  e.pillowUrlList = pillowList[1]
                }
                if (e.woodTexturePicture) {
                  const woodenList = packageImgList(e.woodTexturePicture)
                  e.woodenNameList = [e.woodTexturePicture]
                  e.woodenUrlList = woodenList[1]
                }
                if (e.otherTexturePicture) {
                  const otherList = packageImgList(e.otherTexturePicture)
                  e.otherNameList = [e.otherTexturePicture]
                  e.otherUrlList = otherList[1]
                }
                if (e.problemPicture) {
                  const problemList = packageImgList(e.problemPicture)
                  e.problemNameList = problemList[0]
                  e.problemUrlList = problemList[1]
                }
              })
            } else {
              this.$msg._message(res.error.msg, 'error')
            }
          }
        )
      }
    },
    handleCurrentChange(row, event, column) {
      // 当前表格改变钩子
    },
    handleEdit(index, row) {
      if (!validatenull(row.quantity) && !validatenull(row.price)) {
        row.totalMoney = row.quantity * row.price
      }
    },
    addChildOrder() {
      this.childOrders.push({})
    },
    deleteChildOrder(index) {
      this.childOrders.splice(index, 1)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    uploadSuccess(response) {
      if (response.error.code === HTTPSUCCESS) {
        this.$msg._message('上传成功', 'success')
        response.result.forEach(e => {
          if (e.officialPicture) {
            const officialList = packageImgList(e.officialPicture)
            e.officialNameList = [e.officialPicture]
            e.officialUrlList = officialList[1]
          }
          if (e.productPicture) {
            const productList = packageImgList(e.productPicture)
            e.productNameList = [e.productPicture]
            e.productUrlList = productList[1]
          }
          if (e.mainFabricPicture) {
            const bodyList = packageImgList(e.mainFabricPicture)
            e.bodyNameList = [e.mainFabricPicture]
            e.bodyUrlList = bodyList[1]
          }
          if (e.bolsterFabricPicture) {
            const pillowList = packageImgList(e.bolsterFabricPicture)
            e.pillowNameList = [e.bolsterFabricPicture]
            e.pillowUrlList = pillowList[1]
          }
          if (e.woodTexturePicture) {
            const woodenList = packageImgList(e.woodTexturePicture)
            e.woodenNameList = [e.woodTexturePicture]
            e.woodenUrlList = woodenList[1]
          }
          if (e.otherTexturePicture) {
            const otherList = packageImgList(e.otherTexturePicture)
            e.otherNameList = [e.otherTexturePicture]
            e.otherUrlList = otherList[1]
          }
        })
        this.childOrders = response.result
      }
    },
    modifyAll() {
      const user = JSON.parse(getUser())
      const item = []
      this.childOrders.forEach(childOrder => {
        const a = {
          id: childOrder.id,
          itemNo: childOrder.itemNo,
          brandName: childOrder.brandName,
          quantity: childOrder.quantity,
          price: childOrder.price,
          totalMoney: childOrder.totalMoney,
          storeOrderNo: childOrder.manufactorCode,
          itemHead: [
            {
              key: 'area',
              value: childOrder.area
            },
            {
              key: 'product_name',
              value: childOrder.productName
            },
            {
              key: 'official_picture',
              value: validatenull(childOrder.officialNameList) ? null : childOrder.officialNameList.join(',')
            },
            {
              key: 'product_picture',
              value: validatenull(childOrder.productNameList) ? null : childOrder.productNameList.join(',')
            },
            {
              key: 'size',
              value: childOrder.size
            },
            {
              key: 'sku',
              value: childOrder.sku
            },
            {
              key: 'main_fabric',
              value: childOrder.mainFabric
            },
            {
              key: 'main_fabric_picture',
              value: validatenull(childOrder.bodyNameList) ? null : childOrder.bodyNameList.join(',')
            },
            {
              key: 'bolster_fabric',
              value: childOrder.bolsterFabric
            },
            {
              key: 'bolster_fabric_picture',
              value: validatenull(childOrder.pillowNameList) ? null : childOrder.pillowNameList.join(',')
            },
            {
              key: 'wood_texture',
              value: childOrder.woodTexture
            },
            {
              key: 'wood_texture_picture',
              value: validatenull(childOrder.woodenNameList) ? null : childOrder.woodenNameList.join(',')
            },
            {
              key: 'other_texture',
              value: childOrder.otherTexture
            },
            {
              key: 'other_texture_picture',
              value: validatenull(childOrder.otherNameList) ? null : childOrder.otherNameList.join(',')
            },
            {
              key: 'product_cost',
              value: childOrder.productCost
            },
            {
              key: 'other_cost',
              value: childOrder.extraCost
            },
            {
              key: 'payment_time',
              value: childOrder.paymentTime
            },
            {
              key: 'goods_finish_time',
              value: childOrder.goodsFinishTime
            },
            {
              key: 'arrival_time',
              value: childOrder.arrivalTime
            },
            {
              key: 'laid_up_time',
              value: childOrder.laidUpTime
            },
            {
              key: 'distribution_time',
              value: childOrder.sendTime
            },
            {
              key: 'finish_time',
              value: childOrder.finishedTime
            },
            {
              key: 'problem',
              value: childOrder.problem
            },
            {
              key: 'problem_picture',
              value: validatenull(childOrder.problemNameList) ? null : childOrder.problemNameList.join(',')
            },
            {
              key: 'solved_time',
              value: childOrder.solvedTime
            },
            {
              key: 'solved_plan',
              value: childOrder.solvedPlan
            }
          ]
        }
        item.push(a)
      })
      let amount = 0
      this.childOrders.map(a => a.productCost || 0).concat(this.childOrders.map(a => a.extraCost || 0)).forEach(e => {
        amount += Number(e)
      })
      const params = {
        orderIsSave: true,
        id: this.orderId,
        finalPaymentType: this.order.finCostType,
        salesmanId: this.order.salesmanId,
        salesmanName: this.order.salesmanName,
        finalCostPicture: validatenull(this.order.finCostNameList) ? null : this.order.finCostNameList.join(','),
        head: [
          {
            key: 'contract_money',
            value: this.orderAmount
          },
          {
            key: 'down_payment',
            value: this.customerPay
          },
          {
            key: 'final_payment',
            value: this.finCost
          },
          {
            key: 'contract_time',
            value: this.order.signedTime
          },
          {
            key: 'end_time',
            value: this.order.endedTime
          },
          {
            key: 'contract_picture',
            value: validatenull(this.order.contractNameList) ? null : this.order.contractNameList.join(',')
          },
          {
            key: 'income_prove_picture',
            value: validatenull(this.order.incomeNameList) ? null : this.order.incomeNameList.join(',')
          },
          {
            key: 'report_time',
            value: this.order.reportTime
          },
          {
            key: 'report_advice',
            value: this.order.reportAdvice
          },
          {
            key: 'under_order_time',
            value: this.order.sureOrderTime
          },
          {
            key: 'remark',
            value: this.order.remark
          },
          {
            key: 'pass_proof',
            value: validatenull(this.order.passCerNameList) ? null : this.order.passCerNameList.join(',')
          },
          {
            key: 'child_order_amount',
            value: amount
          },
          {
            key: 'total_cost',
            value: amount
          }
        ],
        customer: this.customer,
        item: item
      }
      this.$http.post(api.ORDER_EDIT, params).then(
        res => {
          if (res.error.code === HTTPSUCCESS) {
            this.$msg._message('操作成功', 'success')
            this.$router.push({
              name: 'order'
            })
          } else {
            this.$msg._message(res.error.message, 'error')
          }
        }
      )
    },
    showImg(url) {
      this.$refs.view.showImg(url)
    }
  }
}
</script>
